﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.DBUtility;
using MM.Model;
using MM.Common;

namespace MM.LibraryAPI
{
    public class CommonConfig
    {
        static CommonConfig()
        {
            Timer = "2";
            NotificationUrl = "http://www.it-leo.com:2002/Manager/NotificationBro.aspx?ID=";
        }
        /// <summary>
        /// 联网时间间隔
        /// </summary>
        public static string Timer
        { get; set; }
        /// <summary>
        /// 通知打开浏览器地址
        /// </summary>
        public static string NotificationUrl
        { get; set; }
    }
    public class Common
    {
        /// <summary>
        /// 当前手机用户不存在则保存
        /// </summary>
        /// <param name="osim"></param>
        /// <param name="ophone"></param>
        /// <param name="oversion"></param>
        public static void CheckMobileUser(MM.Model.simInfo osim, MM.Model.phoneInfo ophone, MM.Model.versionInfo oversion)
        {
            SqlParameter[] parameters = { 
                       new SqlParameter("@IMSI", SqlDbType.VarChar),
                       new SqlParameter("@IMEI", SqlDbType.VarChar),
                       new SqlParameter("@Number", SqlDbType.VarChar),
                       new SqlParameter("@Area", SqlDbType.VarChar),
                       new SqlParameter("@Plmn", SqlDbType.VarChar),
                       new SqlParameter("@Mac", SqlDbType.VarChar),
                       new SqlParameter("@Pid", SqlDbType.VarChar),
                       new SqlParameter("@Appid", SqlDbType.VarChar),
                       new SqlParameter("@Name", SqlDbType.VarChar),
                       new SqlParameter("@Width", SqlDbType.Int),
                       new SqlParameter("@Height", SqlDbType.Int),
                       new SqlParameter("@Position", SqlDbType.VarChar),
                       new SqlParameter("@Version", SqlDbType.VarChar)};
            parameters[0].Value = osim.imsi.ToStringOr();
            parameters[1].Value = osim.imei.ToStringOr();
            parameters[2].Value = osim.number.ToStringOr();
            parameters[3].Value = osim.area.ToStringOr();
            parameters[4].Value = osim.plmn.ToStringOr();
            parameters[5].Value = ophone.mac.ToStringOr();
            parameters[6].Value = ophone.pid.ToStringOr();
            parameters[7].Value = ophone.appid.ToStringOr();
            parameters[8].Value = ophone.name.ToStringOr();
            parameters[9].Value = ophone.width.ToIntOr();
            parameters[10].Value = ophone.height.ToIntOr();
            parameters[11].Value = ophone.position.ToStringOr();
            parameters[12].Value = oversion.code.ToIntOr();

            DbHelperSql.ExecuteNonQuerySP(DbHelperSql.connectionString, "CheckUser", parameters);
        }

        /// <summary>
        /// 设置订单状态为成功，并返回用户购买的物品
        /// </summary>
        /// <param name="OrderID"></param>
        public static void SetOrderStatus(string OrderID)
        {
            OrderInfo mOrderInfo = null;
            using (SqlConnection conn = new SqlConnection(DbHelperSql.connectionString))
            {
                SqlParameter[] parameters = { 
                       new SqlParameter("@OrderID", SqlDbType.VarChar)};
                parameters[0].Value = OrderID;

                using (SqlDataReader dr = DbHelperSql.ExecuteReaderSP(conn, "EditOrderStatus", parameters))
                {
                    if (dr != null && dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            mOrderInfo = DTOHelper.New<OrderInfo>();
                            mOrderInfo.LoadData(dr);
                        }
                    }
                    dr.Close();
                }
            }

            if (mOrderInfo != null)
            {
                if (!string.IsNullOrEmpty(mOrderInfo.Params) && mOrderInfo.Params.Contains("_"))
                {
                    string[] argTemp = mOrderInfo.Params.Split('_');
                    if (argTemp.Length == 3)
                    {
                        string prizeID = argTemp[0];
                        string Mode = argTemp[1];
                        string taskID = argTemp[2];

                        SqlParameter[] parameters = { new SqlParameter("@prizeID", SqlDbType.Int),
                                                        new SqlParameter("@userID", SqlDbType.Int),
                                                        new SqlParameter("@taskID", SqlDbType.Int),
                                                        new SqlParameter("@price", SqlDbType.Int)};
                        parameters[0].Value = prizeID.ToIntOr();
                        parameters[1].Value = mOrderInfo.UserID;
                        parameters[2].Value = taskID.ToIntOr();
                        parameters[3].Value = mOrderInfo.Price;

                        DbHelperSql.ExecuteNonQuerySP(DbHelperSql.connectionStringGtsServer, "UpOrderReturn", parameters);
                    }
                }
            }
        }
    }
}
